#include <QDir>
#include <QFile>
#include <QPixmap>
#include <QStringList>

#include "coutputgenerator.h"

COutputGenerator::COutputGenerator(CTextBrowser *browser, QString dirstr):
        m_dir(dirstr), m_browser(browser), m_current(0)
{
    m_header = readFile("header.html");
    m_footer = readFile("footer.html");
    m_empty = readFile("empty.html");
    m_buttons = readFile("buttons.html");
    m_start = readFile("start.html");
    m_end = readFile("end.html");
    m_field = readFile("field.html");
    m_startScreen = readFile("start_screen.html");

/* Entry types */
    m_entry.insert(CEntryField::Text, readFile("text.html"));
    m_entry.insert(CEntryField::Password, readFile("password.html"));
    m_entry.insert(CEntryField::Image, readFile("image.html"));
    m_entry.insert(CEntryField::Url, readFile("url.html"));
    m_entry.insert(CEntryField::File, readFile("file.html"));
}

QString COutputGenerator::readFile(QString filename)
{
    QDir dir(m_dir);
    QFile file(dir.absoluteFilePath(filename));
    if(file.open(QIODevice::ReadOnly))
    {
        debug(filename);
        QString tmp = QString::fromUtf8(file.readAll());
        file.close();

        return tmp;
    }
    return QString();
}

QString COutputGenerator::generate(CEntry *e, bool show_pass=false)
{
    if(!e)
        return QString();

    m_current = e;
    m_browser->clear();

    QString result = m_header;

    QList<CEntryField*> fields = e->getFields();

//    if(!fields.count())
//        result += m_empty;
//    else
    {
        result += m_start;

        for(int i=0; i<CEntry::columnCount(true); i++)
        {
            QString data = e->getData(i, show_pass).toString();
            if(!data.length())
                data = tr("Empty");

            QString tmp = m_entry.value(CEntryField::Text);
            tmp.replace("%name%", CEntry::getFieldName(i));
            tmp.replace("%value%", data);

            QString row = m_field;

            row.replace("%field%", tmp);
            row.replace("%buttons%", "");

            result += row;
        }

        debug("Fields count: " + QString::number(fields.count()));

        foreach(CEntryField *f, fields)
        {
            QString row = m_field;

            QString tmp = m_entry.value(f->type);

            tmp.replace("%name%", f->name());
            switch(f->type)
            {
                case CEntryField::Image:
                {
                    QPixmap pix;
                    pix.loadFromData(f->value().toByteArray());
                    int num = m_browser->addImage(pix);
                    tmp.replace("%value%", QString::number(num));
                    break;
                }

                case CEntryField::File:
                    tmp.replace("%value%", QString::number(fields.indexOf(f)));
                    break;

                case CEntryField::Password:
                    if(show_pass)
                        tmp.replace("%value%", f->value().toString());
                    else
                        tmp.replace("%value%", "**********");

                default:
                    tmp.replace("%value%", f->value().toString());
            }
            row.replace("%field%", tmp);
            row.replace("%buttons%", createButtons(fields.indexOf(f),
                                                   (f->type!=CEntryField::File && f->type!=CEntryField::Image)));

            result += row;
        }

        result += m_end;
        //debug(tmp);
    }
    result += m_footer;

    return result;
}

QString COutputGenerator::createButtons(int id, bool need_edit)
{
    QString str_delete = m_buttons;
    str_delete.replace("%action%", "delete");
    str_delete.replace("%id%", QString::number(id));
    str_delete.replace("%img%", "field_delete");
    str_delete.replace("%title%", tr("Delete field"));

    if(!need_edit)
        return str_delete;

    QString str_edit = m_buttons;
    str_edit.replace("%action%", "edit");
    str_edit.replace("%id%", QString::number(id));
    str_edit.replace("%img%", "field_edit");
    str_edit.replace("%title%", tr("Edit field"));

    return str_edit + str_delete;
}
